package thread3;

public class Test8 {
    public static void main(String[] args) throws InterruptedException {
        Object object = new Object();

        Thread wait1 = new Thread(() -> {
            synchronized (object) {
                while (true) {
                    System.out.println("wait1 开始等待");
                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println("wait1 等待结束");
                }
            }
        });
        Thread wait2 = new Thread(() -> {
            synchronized (object) {
                while (true) {
                    System.out.println("wait2 开始等待");
                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println("wait2 等待结束");
                }
            }
        });
        Thread wait3 = new Thread(() -> {
            synchronized (object) {
                while (true) {
                    System.out.println("wait3 开始等待");
                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println("wait3 等待结束");
                }
            }
        });

        wait1.start();
        wait2.start();
        wait3.start();

        Thread notifier = new Thread(() -> {
            synchronized (object) {

                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("唤醒开始");
                object.notifyAll();
                System.out.println("唤醒结束");

            }
        });

        notifier.start();

    }
}
